package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;

import neural_network.CommandRunner;

import csv_parser.CSVToMatrix;
import csv_parser.Message;

public class NotReadyStatePanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JButton train, loadBreast, loadRenal, loadLung, goToResults;
	JLabel breastLabel, renalLabel, lungLabel;
	JPanel panel;
	JFileChooser chooser = new JFileChooser();
	final JLabel message;
	BorderedLabel label;

	public NotReadyStatePanel() {
		label = new BorderedLabel(
				"Klasifikator nije spreman. Učitajte podatke.", new Color(
						0xFF6961), 16);
		this.add(label);
		this.setVisible(false);

		message = new JLabel("Podaci nisu učitani");
		JPanel container = new JPanel();
		panel = new JPanel();
		GridLayout gr;
		panel.setLayout(gr = new GridLayout(3, 2));
		gr.setHgap(10);
		gr.setVgap(10);

		loadBreast = new JButton("Učitaj podatke za dojku");
		loadBreast.setName("dojka");
		loadBreast.addActionListener(new LoadAction());
		panel.add(loadBreast);
		breastLabel = new JLabel();
		panel.add(breastLabel);

		loadRenal = new JButton("Učitaj podatke za bubreg");
		loadRenal.setName("bubreg");
		loadRenal.addActionListener(new LoadAction());
		panel.add(loadRenal);
		renalLabel = new JLabel();
		panel.add(renalLabel);

		loadLung = new JButton("Učitaj podatke za pluća");
		loadLung.setName("pluca");
		loadLung.addActionListener(new LoadAction());
		panel.add(loadLung);
		lungLabel = new JLabel();
		panel.add(lungLabel);
		container.add(panel);

		JPanel randompanel = new JPanel();
		randompanel.setPreferredSize(new Dimension(130, 100));
		container.add(randompanel);
		this.add(container);
		train = new JButton("Treniraj neuralnu mrežu");
		train.addActionListener(new TrainAction());
		train.setPreferredSize(new Dimension(200, 50));
		train.setEnabled(false);
		this.add(train);

		goToResults = new JButton("Rezultati");
		goToResults.addActionListener(new ResultsAction());
		goToResults.setPreferredSize(new Dimension(200, 50));
		goToResults.setVisible(false);
		this.add(goToResults);

	}

	public class LoadAction implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			JButton button = (JButton) e.getSource();
			String name = button.getName();
			boolean correct = false;
			File file = null;
			FileNameExtensionFilter filter = new FileNameExtensionFilter(
					"CSV & TXT Files", "csv", "txt");
			chooser.setFileFilter(filter);
			while (!correct) {
				int returnVal = chooser.showOpenDialog(NotReadyStatePanel.this);
				Message msg = new Message();
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					file = chooser.getSelectedFile();
					System.out.println("You chose to open this file: "
							+ chooser.getSelectedFile().getName());
				} else if (returnVal == JFileChooser.CANCEL_OPTION) {
					break;
				}
				if (file != null && CSVToMatrix.isCorrect(file, msg))
					correct = true;
				showMessage(msg);
			}

			if (correct) {
				if (name.equals("dojka")) {
					breastLabel.setIcon(new ImageIcon("icons\\ok28.png"));
				} else if (name.equals("bubreg")) {
					renalLabel.setIcon(new ImageIcon("icons\\ok28.png"));
				} else {
					lungLabel.setIcon(new ImageIcon("icons\\ok28.png"));
				}
			}
			train.setEnabled(true);
			NotReadyStatePanel.this.label.setTextForeground(new Color(0xBDECB6));
			label.setText("Podaci su uspešno učitani");
			if (true) {
			}

		}

		private void showMessage(Message msg) {
			message.setText(msg.message);
		}

	}

	public class TrainAction implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			train.setVisible(false);
			final JLabel label = new JLabel(new ImageIcon("icons\\load.gif"));
			NotReadyStatePanel.this.add(label);
			NotReadyStatePanel.this.label.setTextForeground(new Color(0xBDECB6));
			NotReadyStatePanel.this.label.setText("Treniranje mreže je u toku");
			NotReadyStatePanel.this.revalidate();
			NotReadyStatePanel.this.repaint();
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			ScheduledExecutorService worker = Executors
					.newSingleThreadScheduledExecutor();
			Runnable task = new Runnable() {

				@Override
				public void run() {
					CommandRunner
							.runCommand("octave metastaze.m no_pause no_graphic");
					CommandRunner
							.runCommand("octave metastaze_predictor.m no_pause no_graphic");

					label.setVisible(false);
					goToResults.setVisible(true);
					NotReadyStatePanel.this.label
							.setText("Treniranje mreže je završeno.");
					NotReadyStatePanel.this.validate();

				}
			};

			worker.schedule(task, 1, TimeUnit.SECONDS);

		}

	}

	public class ResultsAction implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			Frame.getFrame().setStatisticPanelVisible(true);
			Frame.getFrame().setNotReadyStatePanelVisible(false);
			train.setVisible(true);
			train.setEnabled(false);

			NotReadyStatePanel.this.validate();

			goToResults.setVisible(false);
			NotReadyStatePanel.this.validate();

		}

	}

}
